// pages/issue/issue.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: [

    ]
  },
  go_goods() {
    wx.navigateTo({
      url: '/pages/goods/goods',
    })
  },
  choose_img() {
    let appInstance = getApp()
    let _this = this
    wx.chooseImage({
      count: 9 - this.data.img.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.uploadFile({
          url: appInstance.globalData.requestUrl+'/wx/common/uploadFile',
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            const data = JSON.parse(res.data)
            let img = _this.data.img
            img.push( data.url)
            _this.setData({ img })
          }
        })
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let args = wx.getStorageSync('args')
    if (args) {
      this.setData({ args })
      console.log(this.data.args,'this.data.args')
    }
    else {
      wx.navigateBack()
      wx.showToast({
        title: '请登录后使用',
        icon: "none"
      })
    }

  },
  async add_img() {
    let img = this.data.img
    let temp = []
    if (img.length == 0) {
      return []
    }
    else {
      for (let i = 0; i < img.length; i++) {
        let temp_id = await wx.cloud.uploadFile({
          cloudPath: "img/" + new Date().getTime() + "-" + Math.floor(Math.random() * 1000),
          filePath: img[i]
        }).then(res => {
          return res.fileID
        })
        temp.push(temp_id)
      }
      return temp
    }
  },
  handle_content(e) {
    this.setData({
      content: e.detail.value
    })
  },
  push() {
    let args = wx.getStorageSync('args')
    let _this = this;
    if (!this.data.content) {
      wx.showToast({
        title: '说点什么吧...',
        icon: "none"
      })
    }
    else {
      wx.showLoading({
        success: res => {
          // this.add_img().then(imgs => {
            // let data = {
            //   imgs,
            //   content: this.data.content,
            //   args: this.data.args
            // }
            // app.get('push_asso', data).then(res => {
            //   wx.hideLoading()
            //   console.log(res);
            //   wx.showToast({
            //     title: res.msg,
            //     icon: "none"
            //   })
            //   setTimeout(() => {
            //     wx.navigateBack()
            //   }, 1000);
            // })
            let appInstance = getApp()
            wx.request({
              url: appInstance.globalData.requestUrl+'/wx/biz/push_asso',
              data:{
                "nickName": _this.data.args.nickName,
                "avatarUrl": _this.data.args.avatarUrl,
                imgs:_this.data.img.toString(),
                content: _this.data.content,
                args: _this.data.args
              },
              method:'POST',
              header: {
                'content-type': 'application/json', // 默认值
                'Authorization': 'Bearer '+ args.loginToken
              },
              success (res) {
                console.log(res.data,'res.data')
                  wx.hideLoading()
                  console.log(res);
                  wx.showToast({
                    title: res.msg,
                    icon: "none"
                  })
                  setTimeout(() => {
                    wx.navigateBack()
                  }, 1000);
              }
            })
          // })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
